package com.xyht.sca_s.student_manage_system.modules.rxRegistrationChargePayment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.ApiModelUtil;
import com.xyht.sca_s.student_manage_system.common.util.TimeUtil;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationAdvance.entity.RxRegistrationAdvanceStudentInfo;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationAdvance.entity.resp.RxRegistrationAdvanceStudentInfoResp;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationAdvance.mapper.RxRegistrationAdvanceStudentInfoMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationChargePayment.entity.RxRegistrationChargePaymentCategoryDivide;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationChargePayment.entity.RxRegistrationChargePaymentRecord;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationChargePayment.entity.req.RxRegistrationChargePaymentRecordExportReq;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationChargePayment.entity.resp.RxRegistrationChargePaymentCategoryDivideExportResp;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationChargePayment.entity.resp.RxRegistrationChargePaymentCategoryDivideResp;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationChargePayment.entity.resp.RxRegistrationChargePaymentRecordExportResp;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationChargePayment.entity.resp.RxRegistrationChargePaymentRecordResp;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationChargePayment.mapper.RxRegistrationChargePaymentCategoryDivideMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationChargePayment.mapper.RxRegistrationChargePaymentRecordMapper;
import com.xyht.sca_s.student_manage_system.modules.rxRegistrationChargePayment.service.RxRegistrationChargePaymentRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.rxRegistrationChargePayment.constant.ExportPaymentDetailConstant.EXPORT_PAYMENT_DETAIL;
import static com.xyht.sca_s.student_manage_system.modules.rxRegistrationChargePayment.constant.PaymentTypeConstant.*;
import static com.xyht.sca_s.student_manage_system.modules.signUp.constant.SmsSignUpCommonConstant.PER_SHEET_NUM;

/**
 * <p>
 * 缴费记录表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-05-13
 */
@Service
@Slf4j
public class RxRegistrationChargePaymentRecordServiceImpl extends ServiceImpl<RxRegistrationChargePaymentRecordMapper, RxRegistrationChargePaymentRecord> implements RxRegistrationChargePaymentRecordService {

    @Resource
    private RxRegistrationChargePaymentRecordMapper rxRegistrationChargePaymentRecordMapper;
    @Resource
    private RxRegistrationChargePaymentCategoryDivideMapper rxRegistrationChargePaymentCategoryDivideMapper;
    @Resource
    private RxRegistrationAdvanceStudentInfoMapper rxRegistrationAdvanceStudentInfoMapper;
    @Resource
    private ApiModelUtil apiModelUtil;

    @Override
    public ResponseResult getPaymentRecordList(Integer pageNum, Integer pageSize, String planId, String type, String middleSchoolId, String majorId, String startDate, String endDate, String name, Integer isFilingStu) {
        Page<RxRegistrationChargePaymentRecord> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<RxRegistrationChargePaymentRecord> lambdaQueryWrapper = new LambdaQueryWrapper<RxRegistrationChargePaymentRecord>()
                .isNotNull(RxRegistrationChargePaymentRecord::getPaymentTime)
                .isNotNull(RxRegistrationChargePaymentRecord::getPaymentAmount)
                .orderByDesc(RxRegistrationChargePaymentRecord::getPaymentTime);
        if (!isNullOrEmpty(planId)) {
            lambdaQueryWrapper.eq(RxRegistrationChargePaymentRecord::getPlanId, planId);
        }

        if (!isNullOrEmpty(type)) {
            lambdaQueryWrapper.eq(RxRegistrationChargePaymentRecord::getType,type);
        }else {
            lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getType, REGISTRATION_APP_TYPE_LIST);
        }
        if (!isNullOrEmpty(isFilingStu)) {
            List<String> studentIdList = rxRegistrationAdvanceStudentInfoMapper.selectList(new LambdaQueryWrapper<RxRegistrationAdvanceStudentInfo>()
                    .like(RxRegistrationAdvanceStudentInfo::getIsFilingStu, isFilingStu))
                    .stream().map(RxRegistrationAdvanceStudentInfo::getId).collect(Collectors.toList());
            if (studentIdList.isEmpty()) {
                lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getStuId,"");
            } else {
                lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getStuId,studentIdList);
            }
        }
        if (!isNullOrEmpty(name)) {
            List<String> studentIdList = rxRegistrationAdvanceStudentInfoMapper.selectList(new LambdaQueryWrapper<RxRegistrationAdvanceStudentInfo>()
                     .like(RxRegistrationAdvanceStudentInfo::getName, name))
                    .stream().map(RxRegistrationAdvanceStudentInfo::getId).collect(Collectors.toList());
            if (studentIdList.isEmpty()) {
                lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getStuId,"");
            } else {
                lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getStuId,studentIdList);
            }
        }

        if (!isNullOrEmpty(majorId)) {
            List<String> studentIdList = rxRegistrationAdvanceStudentInfoMapper.selectList(new LambdaQueryWrapper<RxRegistrationAdvanceStudentInfo>()
                    .eq(RxRegistrationAdvanceStudentInfo::getOrgId, majorId))
                    .stream().map(RxRegistrationAdvanceStudentInfo::getId).collect(Collectors.toList());
            if (studentIdList.isEmpty()) {
                lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getStuId,"");
            } else {
                lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getStuId,studentIdList);
            }
        }
        if (!isNullOrEmpty(middleSchoolId)) {
            List<String> studentIdList = rxRegistrationAdvanceStudentInfoMapper.selectList(new LambdaQueryWrapper<RxRegistrationAdvanceStudentInfo>()
                    .eq(RxRegistrationAdvanceStudentInfo::getMiddleSchoolId, middleSchoolId))
                    .stream().map(RxRegistrationAdvanceStudentInfo::getId).collect(Collectors.toList());
            if (studentIdList.isEmpty()) {
                lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getStuId,"");
            } else {
                lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getStuId,studentIdList);
            }
        }
        if (!isNullOrEmpty(startDate)) {
            lambdaQueryWrapper.ge(RxRegistrationChargePaymentRecord::getPaymentTime,startDate);
        }
        if (!isNullOrEmpty(endDate)) {
            lambdaQueryWrapper.le(RxRegistrationChargePaymentRecord::getPaymentTime,endDate);
        }

        rxRegistrationChargePaymentRecordMapper.selectPage(page,lambdaQueryWrapper);
        List<RxRegistrationChargePaymentRecordResp> respList = page.getRecords()
                .stream()
                .map(rxRegistrationChargePaymentRecord -> {
                    RxRegistrationChargePaymentRecordResp resp = new RxRegistrationChargePaymentRecordResp();
                    RxRegistrationAdvanceStudentInfoResp studentInfoResp = new RxRegistrationAdvanceStudentInfoResp();
                    //缴费总记录
                    BeanUtils.copyProperties(rxRegistrationChargePaymentRecord,resp);
                    //学生信息
                    String stuId = rxRegistrationChargePaymentRecord.getStuId();
                    if (!isNullOrEmpty(stuId)) {
                        RxRegistrationAdvanceStudentInfo rxRegistrationAdvanceStudentInfo = rxRegistrationAdvanceStudentInfoMapper.selectById(stuId);
                        if (!isNullOrEmpty(rxRegistrationAdvanceStudentInfo)) {
                            BeanUtils.copyProperties(rxRegistrationAdvanceStudentInfo,studentInfoResp);
                            studentInfoResp.setStuId(rxRegistrationAdvanceStudentInfo.getId());

                        }
                    }
                    //缴费详情
                    List<RxRegistrationChargePaymentCategoryDivideResp> categoryDivideList = rxRegistrationChargePaymentCategoryDivideMapper.selectList(new LambdaQueryWrapper<RxRegistrationChargePaymentCategoryDivide>()
                            .eq(RxRegistrationChargePaymentCategoryDivide::getPaymentRecordId, rxRegistrationChargePaymentRecord.getId())
                            .orderByDesc(RxRegistrationChargePaymentCategoryDivide::getPaymentTime))
                            .stream()
                            .map(rxRegistrationChargePaymentCategoryDivide -> {
                                    RxRegistrationChargePaymentCategoryDivideResp categoryDivideResp = new RxRegistrationChargePaymentCategoryDivideResp();
                                    BeanUtils.copyProperties(rxRegistrationChargePaymentCategoryDivide,categoryDivideResp);
                                    return categoryDivideResp;
                            })
                                .collect(Collectors.toList());
                    resp.setStudentInfoResp(studentInfoResp);
                    resp.setPaymentCategoryDivideList(categoryDivideList);
                    return resp;
                })
                .collect(Collectors.toList());

        return CommonResult.success(respList,(int)page.getTotal());
    }

    @Override
    @Async
    public Future<ResponseResult> exportStuPaymentInfo(RxRegistrationChargePaymentRecordExportReq exportReq, HttpServletResponse response) {
        try {
            if (isNullOrEmpty(exportReq.getIsExportDetail())) {
                return new AsyncResult<>(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
            }
            LambdaQueryWrapper<RxRegistrationChargePaymentRecord> lambdaQueryWrapper = new LambdaQueryWrapper<RxRegistrationChargePaymentRecord>()
                    .isNotNull(RxRegistrationChargePaymentRecord::getPaymentTime)
                    .isNotNull(RxRegistrationChargePaymentRecord::getPaymentAmount)
                    .orderByDesc(RxRegistrationChargePaymentRecord::getPaymentTime);
            if (!isNullOrEmpty(exportReq.getPlanId())) {
                lambdaQueryWrapper.eq(RxRegistrationChargePaymentRecord::getPlanId, exportReq.getPlanId());
            }

            if (!isNullOrEmpty(exportReq.getType())) {
                lambdaQueryWrapper.eq(RxRegistrationChargePaymentRecord::getType,exportReq.getType());
            }else {
                lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getType, REGISTRATION_APP_TYPE_LIST);
            }

            if (!isNullOrEmpty(exportReq.getName())) {
                List<String> studentIdList = rxRegistrationAdvanceStudentInfoMapper.selectList(new LambdaQueryWrapper<RxRegistrationAdvanceStudentInfo>()
                        .like(RxRegistrationAdvanceStudentInfo::getName, exportReq.getName()))
                        .stream().map(RxRegistrationAdvanceStudentInfo::getId).collect(Collectors.toList());
                if (studentIdList.isEmpty()) {
                    lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getStuId,"");
                } else {
                    lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getStuId,studentIdList);
                }
            }

            if (!isNullOrEmpty(exportReq.getIsFilingStu())) {
                List<String> studentIdList = rxRegistrationAdvanceStudentInfoMapper.selectList(new LambdaQueryWrapper<RxRegistrationAdvanceStudentInfo>()
                        .like(RxRegistrationAdvanceStudentInfo::getIsFilingStu, exportReq.getIsFilingStu()))
                        .stream().map(RxRegistrationAdvanceStudentInfo::getId).collect(Collectors.toList());
                if (studentIdList.isEmpty()) {
                    lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getStuId,"");
                } else {
                    lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getStuId,studentIdList);
                }
            }

            if (!isNullOrEmpty(exportReq.getMajorId())) {
                List<String> studentIdList = rxRegistrationAdvanceStudentInfoMapper.selectList(new LambdaQueryWrapper<RxRegistrationAdvanceStudentInfo>()
                        .eq(RxRegistrationAdvanceStudentInfo::getOrgId, exportReq.getMajorId()))
                        .stream().map(RxRegistrationAdvanceStudentInfo::getId).collect(Collectors.toList());
                if (studentIdList.isEmpty()) {
                    lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getStuId,"");
                } else {
                    lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getStuId,studentIdList);
                }
            }
            if (!isNullOrEmpty(exportReq.getMiddleSchoolId())) {
                List<String> studentIdList = rxRegistrationAdvanceStudentInfoMapper.selectList(new LambdaQueryWrapper<RxRegistrationAdvanceStudentInfo>()
                        .eq(RxRegistrationAdvanceStudentInfo::getMiddleSchoolId, exportReq.getMiddleSchoolId()))
                        .stream().map(RxRegistrationAdvanceStudentInfo::getId).collect(Collectors.toList());
                if (studentIdList.isEmpty()) {
                    lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getStuId,"");
                } else {
                    lambdaQueryWrapper.in(RxRegistrationChargePaymentRecord::getStuId,studentIdList);
                }
            }
            if (!isNullOrEmpty(exportReq.getStartDate())) {
                lambdaQueryWrapper.ge(RxRegistrationChargePaymentRecord::getPaymentTime,exportReq.getStartDate());
            }
            if (!isNullOrEmpty(exportReq.getEndDate())) {
                lambdaQueryWrapper.le(RxRegistrationChargePaymentRecord::getPaymentTime,exportReq.getEndDate());
            }

            List<RxRegistrationChargePaymentRecordExportResp> list = rxRegistrationChargePaymentRecordMapper.selectList(lambdaQueryWrapper)
                    .stream()
                    .map(rxRegistrationChargePaymentRecord -> {
                        RxRegistrationChargePaymentRecordExportResp resp = new RxRegistrationChargePaymentRecordExportResp();
                        BeanUtils.copyProperties(rxRegistrationChargePaymentRecord,resp);
                        resp.setPaymentRecordId(rxRegistrationChargePaymentRecord.getId());
                        String stuId = rxRegistrationChargePaymentRecord.getStuId();
                        if (!isNullOrEmpty(stuId)) {
                            RxRegistrationAdvanceStudentInfo rxRegistrationAdvanceStudentInfo = rxRegistrationAdvanceStudentInfoMapper.selectById(stuId);
                            if (!isNullOrEmpty(rxRegistrationAdvanceStudentInfo)) {
                                BeanUtils.copyProperties(rxRegistrationAdvanceStudentInfo,resp);
                            }
                        }
                        //缴费记录详情
                        List<RxRegistrationChargePaymentCategoryDivideExportResp> categoryDivideList = rxRegistrationChargePaymentCategoryDivideMapper.selectList(new LambdaQueryWrapper<RxRegistrationChargePaymentCategoryDivide>()
                                .eq(RxRegistrationChargePaymentCategoryDivide::getPaymentRecordId, rxRegistrationChargePaymentRecord.getId())
                                .orderByDesc(RxRegistrationChargePaymentCategoryDivide::getPaymentTime))
                                .stream()
                                .map(rxRegistrationChargePaymentCategoryDivide -> {
                                    RxRegistrationChargePaymentCategoryDivideExportResp categoryDivideResp = new RxRegistrationChargePaymentCategoryDivideExportResp();
                                    BeanUtils.copyProperties(rxRegistrationChargePaymentCategoryDivide,categoryDivideResp);
                                    return categoryDivideResp;
                                })
                                .collect(Collectors.toList());
                        resp.setPaymentCategoryDivideList(categoryDivideList);
                        return resp;
                    })
                    .collect(Collectors.toList());
            if (list.size() < 1) {
                return null;
            }
            List<String> paymentRecordIdList = list.stream().map(RxRegistrationChargePaymentRecordExportResp::getPaymentRecordId).collect(Collectors.toList());
            List<String> categoryNameList = rxRegistrationChargePaymentCategoryDivideMapper.selectList(new LambdaQueryWrapper<RxRegistrationChargePaymentCategoryDivide>()
                    .in(RxRegistrationChargePaymentCategoryDivide::getPaymentRecordId,paymentRecordIdList)
                    .groupBy(RxRegistrationChargePaymentCategoryDivide::getCategoryName))
                    .stream()
                    .map(RxRegistrationChargePaymentCategoryDivide::getCategoryName).collect(Collectors.toList());

            //表头数据
            String[] header = exportReq.getLimitColumn().toArray(new String[0]);
            double sheetNum;
            //声明一个工作簿
            HSSFWorkbook workbook = new HSSFWorkbook();

            sheetNum = Math.ceil((double) list.size() / PER_SHEET_NUM);
            for (int k = 0; k < sheetNum; k++) {
                //生成一个表格，设置表格名称为"资源导出表"
                HSSFSheet sheet = workbook.createSheet("学生招生管理缴费流水信息导出表" + (k + 1));

                //设置表格列宽度为10个字节
                sheet.setDefaultColumnWidth(12);
                //创建标题的显示样式
                HSSFCellStyle headerStyle = workbook.createCellStyle();
                headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
                headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

                HSSFFont f = workbook.createFont();
                f.setFontHeightInPoints((short) 10);//字号
                f.setBold(true);//加粗
                headerStyle.setFont(f);
                //创建第一行表头
                HSSFRow headrow = sheet.createRow(0);

                //遍历添加表头(下面模拟遍历学生，也是同样的操作过程)
                for (int i = 0; i < header.length + 1; i++) {
                    //创建一个单元格
                    HSSFCell cell = headrow.createCell(i);

                    if (i == 0) {
                        //创建一个内容对象
                        HSSFRichTextString text = new HSSFRichTextString("序号");
                        //将内容对象的文字内容写入到单元格中
                        cell.setCellValue(text);
                        cell.setCellStyle(headerStyle);
                        continue;
                    }
                    //创建一个内容对象
                    String header1 = apiModelUtil.getFieldValueByFieldName(RxRegistrationChargePaymentRecordExportResp.class, header[i - 1]);
                    HSSFRichTextString text = new HSSFRichTextString(header1);

                    //将内容对象的文字内容写入到单元格中
                    cell.setCellValue(text);
                    cell.setCellStyle(headerStyle);

                }
                //缴费详情表头
                if (exportReq.getIsExportDetail() == EXPORT_PAYMENT_DETAIL) {
                    for (int i = 0; i < categoryNameList.size(); i++) {
                        HSSFCell cell = headrow.createCell(header.length +1+ i);
                        //创建一个内容对象
                        HSSFRichTextString text = new HSSFRichTextString(categoryNameList.get(i));

                        //将内容对象的文字内容写入到单元格中
                        cell.setCellValue(text);
                        cell.setCellStyle(headerStyle);
                    }
                }


                int range = PER_SHEET_NUM;
                if (k + 1 == sheetNum) {
                    range = list.size() - (PER_SHEET_NUM) * k;
                }
                for (int i = 0; i < range; i++) {

                    //创建一行
                    HSSFRow row1 = sheet.createRow(i + 1);
                    RxRegistrationChargePaymentRecordExportResp paymentRecordResp = list.get((PER_SHEET_NUM) * k + i);
                    Map map = JSONObject.parseObject(JSON.toJSONString(paymentRecordResp), Map.class);
                    row1.createCell(0).setCellValue(new HSSFRichTextString(i + 1 + ""));
                    for (int j = 0; j < header.length; j++) {
                        String rowContent = "";
                        if (!isNullOrEmpty(map.get(header[j]))) {
                            if (map.get(header[j]) instanceof Long) {
                                rowContent = TimeUtil.DateAllHanZi((long) map.get(header[j]));
                            } else if ((header[j].equals("type"))) {
                                String type = paymentRecordResp.getType();
                                switch (type) {
                                    case RX_REGISTRATION_CHARGE_PAYMENT_RECORD_TYPE_ADVANCE:
                                        rowContent = "预报名阶段";
                                        break;
                                    case RX_REGISTRATION_CHARGE_PAYMENT_RECORD_TYPE_CONFIRM:
                                        rowContent = "确认报名阶段";
                                        break;
                                    case RX_REGISTRATION_CHARGE_PAYMENT_RECORD_TYPE_WELCOME:
                                        rowContent = "迎新报到阶段";
                                        break;
                                }

                            }else if((header[j].equals("isFilingStu"))){
                                Integer isFilingStu = paymentRecordResp.getIsFilingStu();
                                switch (isFilingStu) {
                                    case 0:
                                        rowContent = "普通学生";
                                        break;
                                    case 1:
                                        rowContent = "建档学生";
                                        break;
                                }

                            } else if((header[j].equals("isStay"))){
                                Integer isStay = paymentRecordResp.getIsStay();
                                switch (isStay) {
                                    case 0:
                                        rowContent = "未住校";
                                        break;
                                    case 1:
                                        rowContent = "住校";
                                        break;
                                }

                            } else {
                                rowContent = String.valueOf(map.get(header[j]));
                            }
                        }
                        row1.createCell(j + 1).setCellValue(new HSSFRichTextString(rowContent));
                    }

                    //缴费详情表头
                    if (exportReq.getIsExportDetail() == EXPORT_PAYMENT_DETAIL) {
                        for (int n = 0; n < categoryNameList.size(); n++) {
                            List<RxRegistrationChargePaymentCategoryDivideExportResp> paymentCategoryDivideList = paymentRecordResp.getPaymentCategoryDivideList();
                            if (paymentCategoryDivideList != null && paymentCategoryDivideList.size() > 0) {
                                int origin_num = header.length +1+ n;
                                int finalN = n;
                                List<RxRegistrationChargePaymentCategoryDivideExportResp> paymentCategoryDivideExportList = paymentCategoryDivideList.stream()
                                        .filter(a -> a.getCategoryName().equals(categoryNameList.get(finalN))).collect(Collectors.toList());
                                if (paymentCategoryDivideExportList.size() > 0) {
                                    row1.createCell(origin_num).setCellValue(new HSSFRichTextString(String.valueOf(paymentCategoryDivideExportList.get(0).getAmount())));

                                }

                            }
                        }

                    }

                }
            }



            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("学生招生管理缴费流水信息导出表.xlsx", "utf-8"));
            response.setCharacterEncoding("utf-8");
            response.setHeader("file-type", "download");
            response.setHeader("file-name", URLEncoder.encode("学生招生管理缴费流水信息导出表.xlsx", "utf-8"));
            response.setHeader("Access-Control-Expose-Headers", "file-type,file-name");
            //刷新缓冲
            response.flushBuffer();
            //workbook将Excel写入到response的输出流中，供页面下载
            OutputStream os = response.getOutputStream();
            workbook.write(os);

            workbook.close();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
            return new AsyncResult<>(CommonResult.failed(CommonCodeEnum.FAIL));
        }
        return null;
    }

//    private void addPaymentDetailTableHead(HSSFRow headRow, HSSFCellStyle headerStyle, Integer origin_cell_num, String categoryDetailName) {
//        if (!isNullOrEmpty(categoryDivideExportResp.getCategoryDetailName())) {
//            setTableHead(headRow, headerStyle, origin_cell_num , categoryDivideExportResp.getCategoryDetailName() + "缴费金额");
//        } else {
//            setTableHead(headRow, headerStyle, origin_cell_num , categoryDivideExportResp.getCategoryName() + "缴费金额");
//        }
//
//    }
//
//    private void setTableHead(HSSFRow headRow, HSSFCellStyle headerStyle, Integer cell_num, String headContent) {
//        HSSFCell cell = headRow.createCell(cell_num);
//        //创建一个内容对象
//        HSSFRichTextString text = new HSSFRichTextString(headContent);
//
//        //将内容对象的文字内容写入到单元格中
//        cell.setCellValue(text);
//        cell.setCellStyle(headerStyle);
//    }



}
